package com.luo.shop.controller.admin;

import com.luo.shop.pojo.Category;
import com.luo.shop.pojo.Page;
import com.luo.shop.pojo.User;
import com.luo.shop.service.admin.CategoryAdminService;
import com.luo.shop.service.admin.CommentAdminService;
import com.luo.shop.service.admin.GoodsAdminService;
import com.luo.shop.service.admin.UserAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("admin")
public class SystemAdminController {
    @Autowired
    private CommentAdminService commentAdminService;
    @Autowired
    private UserAdminService userAdminService;
    @Autowired
    private GoodsAdminService goodsAdminService;
    @Autowired
    private CategoryAdminService categoryAdminService;

    @GetMapping("login")
    public String adminLogin(HttpSession session){   //后台管理登录页面
        session.setAttribute("adminUser",null);
        session.setAttribute("adminLogin",null);
        return "admin/login";
    }

    @GetMapping("/")
    public String adminIndex(){   //后台管理首页页面
        return "admin/index";
    }

    @GetMapping("welcome")
    public String adminWelcome(Model model){  //后台管理欢迎页面
        model.addAttribute("commentCount",commentAdminService.selectCount());
        model.addAttribute("goodsCount",goodsAdminService.selectCount());
        model.addAttribute("userCount",userAdminService.selectCount());
        return "admin/welcome";
    }

    @GetMapping("admin-info")
    public String adminInfo(HttpSession session){   //后台管理用户个人信息页面
        User adminUser = (User)session.getAttribute("adminUser");
        session.setAttribute("adminUser",userAdminService.querbyById(adminUser.getId()));
        return "admin/admin-info";
    }

    @GetMapping("comment")   //评论信息管理页面
    public String AdminComment(
            @RequestParam(required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(required = false) String info,
            Model model
    ){
        model.addAttribute("page",commentAdminService.selectAll(pageNum,info));
        return "admin/comment";
    }

    @GetMapping("user")
    public String AdminUser(  //用户信息管理页面
            @RequestParam(required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(required = false) String sex, @RequestParam(required = false) String username,
            Model model,HttpSession session
    ){
        Page<User> userPage = userAdminService.selectAll(pageNum, sex, username);
        model.addAttribute("page",userPage);
        session.setAttribute("userPageNum",userPage.getPageNum());
        return "admin/user";
    }

    @GetMapping("userAdd")
    public String AdminuserEdit(Model model){   //添加用户信息页面
        return "admin/user-add";
    }

    @GetMapping("userUpdate")
    public String AdminuserEdit(Model model,Long id){   //编辑用户信息页面
        model.addAttribute("user",userAdminService.querbyById(id));
        return "admin/user-update";
    }

    @GetMapping("category")
    public String adminCategory(Model model){   //分数管理页面
        model.addAttribute("categorys",categoryAdminService.selectAll());
        return "admin/category";
    }

    @GetMapping("categoryAdd")
    public String adminCategoryAdd(Long id,Model model){   //添加分类信息页面
        for (Category category : categoryAdminService.selectAll()) {
            if (category.getId()==id){
                model.addAttribute("category",category);
                break;
            }else{
                model.addAttribute("category",new Category());
            }
        }
        return "admin/category-add";
    }

    @GetMapping("categoryEdit")
    public String adminCategoryEdit(Long id,Model model){   //添加分类信息页面
        for (Category category : categoryAdminService.selectAll()) {
            if (category.getId()==id){
                model.addAttribute("category",category);
                break;
            }
        }
        return "admin/category-edit";
    }

    @GetMapping("goods")
    public String adminGoods(Model model,
                           @RequestParam(required = false)  String title,
                             @RequestParam(required = false)   Long cid,
                             @RequestParam(defaultValue = "1",required = false) Integer pageNum,
                             @RequestParam(required = false) Boolean isputaway
                             ,HttpSession session
    ){
        model.addAttribute("categorys",categoryAdminService.selectAll());
        model.addAttribute("page",goodsAdminService.selectAll(title,cid,pageNum,isputaway));
        session.setAttribute("goodsPageNum",pageNum);
        return "admin/goods";
    }

    @GetMapping("goodsAdd")
    public String adminGoods(Model model){
        model.addAttribute("categorys",categoryAdminService.selectAll());
        return "admin/goods-add";
    }

    @GetMapping("goodsUpdate")
    public String adminGoods(Model model,Long id){
        model.addAttribute("categorys",categoryAdminService.selectAll());
        model.addAttribute("goods",goodsAdminService.querbyById(id));
        return "admin/goods-update";
    }

}
